package fr.webeo.teambuilder.bo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The team business object.
 * 
 * @author Sébastien Bordes
 * @version $Revision$ $Date$ $Name$
 * 
 * @since TeamBuilder 1.0
 */
public class Team {

    /** The team name. */
    private String name = null;

    /** The list of players. */
    private List<Player> players = null;

    /** The captain of the team. */
    private Player captain = null;

    /**
     * Constructor of team object.
     * 
     * @param name the name of the team
     */
    public Team(String name) {
        setName(name);
        this.players = new ArrayList<Player>();
    }

    /**
     * @return Returns the name.
     */
    public String getName() {
        return this.name;
    }

    /**
     * @param name The name to set.
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return Returns the players.
     */
    public void addPlayer(Player p) {
        this.players.add(p);
        Collections.sort(this.players);
    }

    /**
     * @return Returns the players.
     */
    public void removePlayer(Player p) {
        this.players.remove(p);
        if (getCaptain() != null && getCaptain().equals(p)) {
            if (getPlayers().size() > 0) {
                setCaptain(getPlayers().get(0));
            } else {
                setCaptain(null);
            }
        }
    }

    /**
     * @return Returns the players.
     */
    public List<Player> getPlayers() {
        return this.players;
    }

    /**
     * @param players The players to set.
     */
    public void setPlayers(List<Player> players) {
        this.players = players;
    }

    /**
     * @return Returns the captain.
     */
    public Player getCaptain() {
        return this.captain;
    }

    /**
     * @param captain The captain to set.
     */
    public void setCaptain(Player captain) {
        this.captain = captain;
    }
}
